package com.chenjj.bigdata.hbase.client;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public abstract class AbstractHbaseClient implements HbaseClient{
	
	private ExecutorService executor;
	
	private Configuration configuration;
	
	private Connection connection;
	
	private Logger logger = LoggerFactory.getLogger(getClass());
		
	protected AbstractHbaseClient(int _poolSize,String _zkAdress,String _zkPort,String _regionPrincipal,
												  String _masterPrincipal, String _javaKrb5Path,String _keytabPrincipal,
												  String _keytabPath){
		initHbaseClient(_poolSize, _zkAdress, _zkPort, _regionPrincipal, _masterPrincipal, _javaKrb5Path, _keytabPrincipal, _keytabPath);       
	}

    protected AbstractHbaseClient(String _zkAdress,String _zkPort) {
        initHbaseClient(20,_zkAdress,_zkPort,"","","","","");
    }

	
	protected AbstractHbaseClient(int _poolSize, String _zkAdress,String _zkPort) {
		initHbaseClient(_poolSize,_zkAdress,_zkPort,"","","","","");
	}
	
	private void initHbaseClient(int _poolSize,String _zkAdress,String _zkPort,String _regionPrincipal,
			  String _masterPrincipal, String _javaKrb5Path,String _keytabPrincipal,
			  String _keytabPath){
		try {
			executor = Executors.newFixedThreadPool(_poolSize);
			configuration = HBaseConfiguration.create();
	        configuration.set("hbase.zookeeper.quorum", _zkAdress);
	        configuration.set("hbase.zookeeper.property.clientPort", _zkPort);
	        //TODO 添加权限相关参数
		} catch (NullPointerException e) {
			logger.info("_poolSize , _zkAdress , _zkPort can not be null",e);
			throw e;
		}

        try {
			this.connection = ConnectionFactory.createConnection(configuration,executor);
			logger.info("HbaseClient init success");
		} catch (IOException e) {
			logger.info("HbaseClient init failed",e);
			e.printStackTrace();
			throw new RuntimeException(e);
		} 
	}

	public Connection getConnection() {
		return connection;
	}
	
	protected ResultScanner getResultScanner(String tableName, Scan scan) throws IOException{
    	Table table = null;
    	ResultScanner resultScanner = null;
    	
    	try {
			table = getConnection().getTable(TableName.valueOf(tableName.getBytes()));
			resultScanner = table.getScanner(scan);
		} catch (IllegalArgumentException e1) {
			logger.error("",e1);
			e1.printStackTrace();
			throw new IllegalArgumentException(e1);
		} catch (IOException e1) {
			logger.error("",e1);
			throw new IOException(e1);
		}finally{
			try {
				if(null != table){
					table.close();
				}	
			} catch (IOException e) {
				logger.error("",e);
				e.printStackTrace();
			}
		}
		return resultScanner;
	}
	
	protected void destroy(){
		try {
			connection.close();
		} catch (IOException e) {
			logger.info("HbaseClient close failed",e);
			e.printStackTrace();
		}
	}
}
